﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Configuration;
using System.ComponentModel;

namespace DarkLakeTools.ImageText
{
    /// <summary>
    /// 
    /// </summary>
    public class ImageText : IHttpHandler
    {
        #region IHttpHandler Members

        /// <summary>
        /// Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler"/> instance.
        /// </summary>
        /// <value></value>
        /// <returns>true if the <see cref="T:System.Web.IHttpHandler"/> instance is reusable; otherwise, false.</returns>
        public bool IsReusable
        {
            get { return false; }
        }

        /// <summary>
        /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface.
        /// </summary>
        /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        public void ProcessRequest(HttpContext context)
        {
            DarkLakeTools.Configuration.ImageText configSection = null; 

            try 
            {

                configSection = ((DarkLakeTools.Configuration.DarkLakeSection)(ConfigurationManager.GetSection("darkLakeSection"))).ImageText;
            }
            catch 
            {
                context.Response.Clear(); 
                context.Response.Write("Configuration Error"); 
                context.Response.StatusCode = 500; 
                context.Response.Flush(); 
                context.Response.End(); 
            }

            FontStyle fontStyle = FontStyle.Regular;
            string key = context.Request.QueryString["style"] ?? "";
            string fontFamily;
            string text = context.Request.QueryString["Text"] ?? "";
            System.Drawing.Color Color, BackColor;
            
            int width = 0;
            int height; 

            if (string.IsNullOrEmpty(key))
            {
                fontFamily = context.Request["FontFamily"] ?? "";

                width = Convert.ToInt16(context.Request.QueryString["w"] ?? "0");
                height = Convert.ToInt16(context.Request.QueryString["h"] ?? "0");
                Color = System.Drawing.Color.FromName(context.Request.QueryString["c"]);
                BackColor = System.Drawing.Color.FromName(context.Request.QueryString["b"]); 
            }
            else
            {
                height = configSection.ImageTextDefaults.GetItemByKey(key).Height;
                fontFamily = configSection.ImageTextDefaults.GetItemByKey(key).FontFamily;
                TypeConverter tc = TypeDescriptor.GetConverter(typeof(FontStyle)); 

                
                fontStyle = (FontStyle)tc.ConvertFromString(configSection.ImageTextDefaults.GetItemByKey(key).FontStyle);
                Color = System.Drawing.Color.FromName(configSection.ImageTextDefaults.GetItemByKey(key).ForeColor);
                BackColor = System.Drawing.Color.FromName(configSection.ImageTextDefaults.GetItemByKey(key).BackColor); 
            }

            int offSetX = 1;
            int offSetY = 1;

            Font font = new Font(fontFamily, height, fontStyle, GraphicsUnit.Pixel);

            if (width == 0)
            {
                Graphics gp = Graphics.FromImage(new Bitmap(1, 1));
                width = (int)(gp.MeasureString(text, font).Width);
            }
            
            Bitmap Bitmap = new Bitmap(width, height);
            Graphics Graphic = Graphics.FromImage(Bitmap);

            SolidBrush ForeBrush = new SolidBrush(Color);
            SolidBrush BackBrush = new SolidBrush(BackColor);

            Graphic.FillRectangle(BackBrush, 0, 0, width, height);
            Graphic.TextRenderingHint = TextRenderingHint.AntiAlias;

            PointF Point = new PointF(offSetX, offSetY);
            //Font font = new Font(FontFamily.GenericSansSerif, Convert.ToInt16(Convert.ToInt16(context.Request.QueryString["h"]) * .75), FontStyle.Italic, GraphicsUnit.Pixel);

            Graphic.DrawString(text, font, ForeBrush, new PointF(offSetX, offSetY));

            context.Response.ContentType = "image/jpeg";
            Bitmap.Save(context.Response.OutputStream, ImageFormat.Jpeg);

        }

        #endregion
    }
}

